//
//  UIButton.h
//  storm
//
//  Created by wangdi on 13-8-3.
//
//

#ifndef __storm__UIButton__
#define __storm__UIButton__

#include "UIWidget.h"

namespace storm
{
    class UIButton : public UIWidget
    {
    public:
        enum ButtonType
        {
            TYPE_NULL,			// 神马都没有，可以响应一个区域
            TYPE_NONE,			// 点击没有任何动作
            TYPE_SCALE,			// 点击时放大，失去焦点时缩小为原来比例
            TYPE_TRANSPARENT,	// 点击时显示图片，否则隐藏
            TYPE_CALLSHOW,		// 永远不会显示图片，除非调用显示函数，用于做复选按钮
            TYPE_REPLACE,		// 击中状态时改变图片，失去点击则换回原图片
            TYPE_CHANGE,		// 点击后按钮图片改变，再次点击变回
            TYPE_CALLCHANGE,    // 被动变换图片
            TYPE_CALLSCALE,     // 被动缩放
            TYPE_OPACITY,
        };
        
        enum State
        {
            STATE_NORMAL,
            STATE_CLICKING,
            STATE_CLICKED,
        };
        
    public:
        static UIButton *Create(const char *szNormalName = NULL, bool bUseFrame =false, cocos2d::CCSize size = cocos2d::CCSizeZero);
        
    public:
        sigslot::signal1<UIButton *> OnClick;
    private:
        CREATE_FUNC(UIButton);
        virtual UIWidget *onReceiveNewTouch(cocos2d::CCTouch *pTouch);
        virtual void onCurTouchMoveOut(cocos2d::CCTouch *pTouch);
        virtual void onCurTouchFinish(cocos2d::CCTouch *pTouch);
        
        virtual void loseFocus();
        
    private:
        // 将按钮恢复为未点击状态。
        void ResetState();
    public:
        
        // 静态构造BUTTON
        // 如果TYPE为 NULL 或 NONE，则可以不设置任何文理
        // 如果TYPE为 SCALE，TRANSPARENT,CALLSHOW，则必须加载texture1
        // 其他类型必须加载texture1和texture2
        UIButton();
        virtual ~UIButton() {}
        
        float      ScaleFactor() const   {return m_scaleFactor;}
        float      OpacityFactor() const {return m_opacityFactor;}
        
        cocos2d::CCSprite *NormalSprite()   {return m_pSprNormal;}
        cocos2d::CCSprite *ClickedSprite()  {return m_pSprClicked;}
        cocos2d::CCSprite *ClickingSprite() {return m_pSprClicking;}
        void               SetState(State state);
        
        // 设置缩放因数，影响TYPE_SCALE
        void SetScaleFactor(float scale) ;
        void SetScaleNormal(float scale) ;
        void SetOpacityFactor(float scale);
        void SetNormalSprite(const char *szName, bool bUseFrame = false);
        void SetClickedSprite(const char *szName, bool bUseFrame = false);
        void SetClickingSprite(const char *szName, bool bUseFrame = false);
        
    protected:
        void SetSprite(cocos2d::CCSprite *&pSpr, const char *szName, bool bUseFrame);
    private:
        cocos2d::CCSprite *m_pSprClicking;
        cocos2d::CCSprite *m_pSprClicked;
        cocos2d::CCSprite *m_pSprNormal;
        cocos2d::CCLabelBMFont *m_pFntReplace;
        cocos2d::CCLabelBMFont *m_pFntNormal;
        
        float              m_scaleFactor;
        float              m_scaleNormal;
        float              m_opacityFactor;
    };
}

#endif /* defined(__storm__UIButton__) */
